import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        // 是否登录
        isLogin: false,

        //  用户token
        userToken: localStorage.getItem('userToken') ? localStorage.getItem('userToken') : '',
        //  修改某篇文章
        currentPage: localStorage.getItem('val') ? localStorage.getItem('val') : [],

        value: JSON.parse(localStorage.getItem('val')) ? JSON.parse(localStorage.getItem('val')) : [],
        //  用户信息
        userInfo: JSON.parse(localStorage.getItem('info')) ? JSON.parse(localStorage.getItem('info')) : [],
        articleList: localStorage.getItem('list') ? localStorage.getItem('list'): [],
    },
    mutations: {
        saveToken(state, data) {
            state.userToken = data.token;
            localStorage.setItem('userToken', data.token);
            localStorage.setItem('isLogin', true);
        },
        // 文章信息
        saveItem(state, val) {
            state.value = val.value
            localStorage.setItem('val', JSON.stringify(state.value))
        },
        //  获取搜索文章列表
        saveList(state, val) {
            state.articleList = val.list
            localStorage.setItem('list', JSON.stringify(state.articleList))

        },
        //  用户信息
        saveUser (state, data) {
            state.userInfo = data.info
            localStorage.setItem('info', JSON.stringify(state.userInfo))
        }
    },
    actions: {}
})
